﻿using Sunny.UI;

namespace supermarket.page
{
    public partial class allsupplier : UIPage
    {
        public UIDataGridView dataGridView;
        public allsupplier()
        {
            InitializeComponent();
        }

        private void allsupplier_Load(object sender, EventArgs e)
        {
            dataGridView = uiDataGridView1;
            res res = HTTPUtils.GET("supplier/all");
            dataGridView.DataSource = res.date;
            dataGridView.ReadOnly = true;
        }

        private void uiButton1_Click(object sender, EventArgs e)
        {
            res res = HTTPUtils.GET("supplier/all");
            dataGridView.DataSource = res.date;
        }

        private void setup(object sender, DataGridViewCellEventArgs e)
        {
            supplier supplier = new supplier();
            supplier.name = uiDataGridView1.CurrentCell.OwningRow.Cells[1].Value.ToString();
            supplier.addr = uiDataGridView1.CurrentCell.OwningRow.Cells[2].Value.ToString();
            supplier.phone = uiDataGridView1.CurrentCell.OwningRow.Cells[3].Value.ToString();
            supplier.goods = uiDataGridView1.CurrentCell.OwningRow.Cells[4].Value.ToString();
            UIEditForm frm = form.supplierform("修改供应商", supplier);
            frm.Render();
            frm.ShowDialog();
            if (frm.IsOK)
            {
                res res = HTTPUtils.jsonPost("supplier/upset", new
                {
                    id = uiDataGridView1.CurrentCell.OwningRow.Cells[0].Value.ToString(),
                    name = frm["name"],
                    addr = frm["addr"],
                    phone = frm["phone"],
                    goods = frm["goods"],
                });
                if (res.cod != "200")
                {

                    MessageBox.Show(res.msg, "修改供应商发送错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }
                else
                {
                    uiButton1_Click(sender, e);
                    MessageBox.Show(res.msg, "修改供应商成功", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
        }

        private void uiDataGridView1_CellMouseMove(object sender, DataGridViewCellMouseEventArgs e)
        {

        }

        private void uiDataGridView1_CellMouseUp(object sender, DataGridViewCellMouseEventArgs e)
        {
            if (e.Button == MouseButtons.Right)
            {
                String re = MessageBox.Show("是否真的删除！", "错误提示", MessageBoxButtons.YesNo, MessageBoxIcon.Information).ToString();
                if (re == "Yes")
                {
                    res res = HTTPUtils.jsonPost("supplier/remove", new
                    {
                        id = uiDataGridView1.CurrentCell.OwningRow.Cells[0].Value.ToString(),
                        name = uiDataGridView1.CurrentCell.OwningRow.Cells[1].Value.ToString(),
                        addr = uiDataGridView1.CurrentCell.OwningRow.Cells[2].Value.ToString(),
                        phone = uiDataGridView1.CurrentCell.OwningRow.Cells[3].Value.ToString(),
                        goods = uiDataGridView1.CurrentCell.OwningRow.Cells[4].Value.ToString(),
                    });
                    if (res.cod != "200")
                    {

                        MessageBox.Show(res.msg, "删除供应商发送错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        return;
                    }
                    else
                    {
                        uiButton1_Click(sender, e);
                        MessageBox.Show(res.msg, "删除供应商成功", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                }
                return;
            }
        }

        private void allsupplier_Initialize(object sender, EventArgs e)
        {

        }
    }
}
